package cn.itcast.web.controller.system;


import cn.itcast.domain.system.Module;
import cn.itcast.service.system.ModuleService;
import cn.itcast.web.controller.BaseController;
import com.alibaba.excel.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.util.calendar.BaseCalendar;

import java.util.List;

@Controller
@RequestMapping("/system/module")
public class ModuleController extends BaseController {

    @Autowired
    private ModuleService moduleService;

    @RequestMapping("/list")
    public String list(@RequestParam(defaultValue = "1")int page,@RequestParam(defaultValue = "5")int size){
        PageInfo info = moduleService.findAll(page,size);
        request.setAttribute("page",info);
        return "system/module/module-list";
    }

    //进入添加
    @RequestMapping("/toAdd")
    public String toAdd(){
        List<Module> list = moduleService.findAll();
        request.setAttribute("menus",list);
        return "system/module/module-add";
    }

    //添加
    @RequestMapping("/edit")
    public String edit(Module module){
        if (StringUtils.isEmpty(module.getId())){
            moduleService.save(module);
        }else {
            moduleService.update(module);
        }

        return "redirect:/system/module/list.do";
    }

    //进入修改
    @RequestMapping("/toUpdate")
    public String toUpdate(String id){

        Module module = moduleService.findById(id);
        request.setAttribute("module",module);

        return "system/module/module-update";
    }

    //删除
    @RequestMapping("/delete")
    public String delete(String id){
        moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }

}
